//
//  YZYSonUserListCell.m
//  YZYSDKManagerDemo
//
//  Created by 赵隆杰 on 2024/6/11.
//

#import "YZYSonUserListCell.h"
#import "YZYSDKManagerHeader.h"
#import "YZYSaccountStore.h"
#import "ZCFunDataFormat.h"

@interface YZYSonUserListCell ()<UITextViewDelegate>

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIImageView *selectImage;
@property (nonatomic, strong) UILabel *defaultLabel;
@property (nonatomic, strong) UILabel *sonNameLabel;

@property (nonatomic, assign) NSInteger index;

@end

@implementation YZYSonUserListCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColor.clearColor;
        [self drawIt];
    }
    
    return self;
}

- (void)drawIt {
    UIView *titleView = [[UIView alloc] init];
    titleView.layer.masksToBounds = YES;
    titleView.layer.cornerRadius = 4;
    titleView.layer.borderWidth = 1;
    [self.contentView addSubview:titleView];
    titleView.frame = CGRectMake(20, 5, popupW - 40, 34);
    self.titleView = titleView;
    
    UIImageView *selectImage = [[UIImageView alloc] init];
    [titleView addSubview:selectImage];
    selectImage.frame = CGRectMake(10, 10, 12, 12);
    self.selectImage = selectImage;
    
    UIImageView *arrowImage = [[UIImageView alloc] init];
    [titleView addSubview:arrowImage];
    arrowImage.image = [[YZYSaccountStore sharedInstance] getImage:@"arrow_right"];
    arrowImage.frame = CGRectMake(titleView.frame.size.width - 20 - 5, 10, 5, 10);
    
    UILabel *defaultLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(selectImage.frame) + 10, 0, 0, titleView.frame.size.height)];
    defaultLabel.font = [UIFont boldSystemFontOfSize:16];
    defaultLabel.text = @"默认";
    defaultLabel.textColor = [UIColor colorWithRed:81/255.0 green:184/255.0 blue:191/255.0 alpha:1.0];
    [titleView addSubview:defaultLabel];
    CGSize defaultLabelSize = [defaultLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: defaultLabel.font} context:nil].size;
    defaultLabel.frame = CGRectMake(defaultLabel.frame.origin.x, defaultLabel.frame.origin.y, defaultLabelSize.width, defaultLabel.frame.size.height);
    self.defaultLabel = defaultLabel;
    
    UILabel *sonNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(defaultLabel.frame) + 5, 0, arrowImage.frame.origin.x - 5 - (CGRectGetMaxX(defaultLabel.frame) + 5), titleView.frame.size.height)];
    [titleView addSubview:sonNameLabel];
    sonNameLabel.textColor = [UIColor colorWithRed:21/255.0 green:20/255.0 blue:20/255.0 alpha:1.0];
    sonNameLabel.font = [UIFont systemFontOfSize:16];
    sonNameLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
    [sonNameLabel addGestureRecognizer:tapGesture];
    self.sonNameLabel = sonNameLabel;
}

- (void)dataWithDict:(NSDictionary *)dict withIndex:(NSInteger)index withSelectIndex:(NSInteger)selectIndex {
    _index = index;
    NSNumber *isDefault = [ZCFunDataFormat filterNumber:dict[@"isDefault"]];
    NSString *sonNickName = dict[@"sonNickName"];
    if (index == selectIndex) {
        self.titleView.layer.borderColor = [UIColor colorWithRed:81/255.0 green:184/255.0 blue:191/255.0 alpha:1.0].CGColor;
        self.titleView.backgroundColor = [UIColor colorWithRed:233/255.0 green:249/255.0 blue:250/255.0 alpha:1.0];
        self.selectImage.image = [[YZYSaccountStore sharedInstance] getImage:@"round_selected"];
    } else {
        self.titleView.layer.borderColor = [UIColor colorWithRed:185/255.0 green:185/255.0 blue:185/255.0 alpha:1.0].CGColor;
        self.titleView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        self.selectImage.image = [[YZYSaccountStore sharedInstance] getImage:@"round_unselected"];
    }
    self.sonNameLabel.text = sonNickName;
    CGSize sonNameLabelSize = [self.sonNameLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: self.sonNameLabel.font} context:nil].size;
    if (isDefault.boolValue) {
        self.defaultLabel.text = @"默认";
        CGSize defaultLabelSize = [self.defaultLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: self.defaultLabel.font} context:nil].size;
        self.defaultLabel.frame = CGRectMake(self.defaultLabel.frame.origin.x, self.defaultLabel.frame.origin.y, defaultLabelSize.width, self.defaultLabel.frame.size.height);
        self.sonNameLabel.frame = CGRectMake(CGRectGetMaxX(self.defaultLabel.frame) + 5, self.sonNameLabel.frame.origin.y, sonNameLabelSize.width, self.sonNameLabel.frame.size.height);
    } else {
        self.defaultLabel.text = @"";
        self.defaultLabel.frame = CGRectMake(self.defaultLabel.frame.origin.x, self.defaultLabel.frame.origin.y, 0, self.defaultLabel.frame.size.height);
        self.sonNameLabel.frame = CGRectMake(CGRectGetMaxX(self.defaultLabel.frame), self.sonNameLabel.frame.origin.y, sonNameLabelSize.width, self.sonNameLabel.frame.size.height);
    }
}

// 实现点击事件的响应方法
- (void)labelTapped:(UITapGestureRecognizer *)gesture {
    // 在这里处理点击事件
    UILabel *tappedLabel = (UILabel *)gesture.view;
    NSLog(@"Label clicked: %@", tappedLabel.text);
    NSLog(@"编辑小号 - %ld", self.index);
    if ([self.delegate respondsToSelector:@selector(sonUserNameDidSelect:)]) {
        [self.delegate sonUserNameDidSelect:self.index];
    }
}

@end
